package com.mdxq.mall.product.controller;

import cn.hutool.core.util.StrUtil;
import com.mdxq.mall.response.R;
import com.mdxq.mall.vo.ProductBaseInfoVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.product.entity.Product;
import com.mdxq.mall.product.service.ProductService;

import java.util.Arrays;
import java.util.List;

/**
 * 商品信息 控制层。
 *
 * @author zdy
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 添加商品信息。
     *
     * @param product 商品信息
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public R save(@RequestBody Product product) {
        return R.ok(productService.save(product));
    }

    /**
     * 根据主键删除商品信息。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Integer id) {
        return productService.removeById(id);
    }

    /**
     * 根据主键更新商品信息。
     *
     * @param product 商品信息
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    public R update(@RequestBody Product product) {
        return R.ok(productService.updateById(product));
    }

    /**
     * 查询所有商品信息。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Product> list() {
        return productService.list();
    }

    /**
     * 根据商品信息主键获取详细信息。
     *
     * @param id 商品信息主键
     * @return 商品信息详情
     */
    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable Integer id) {
        return R.ok(productService.getById(id));
    }
    @GetMapping("getBaseInfo/{id}")
    public R getBaseInfo(@PathVariable Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        queryWrapper.select("id","name","product_sn","price","stock");
        return R.ok(productService.getOneAs(queryWrapper, ProductBaseInfoVo.class));
    }

    /**
     * 分页查询商品信息。
     * @param page 分页对象
     * @param keyword 关键字
     * @param productSn 商品sn编码
     * @param productCategoryId 商品类别id
     * @param brandId 品牌id
     * @param publishStatus 上架状态
     * @param verifyStatus 审核状态
     * @return
     */
    @GetMapping("page")
    public R page(Page<Product> page,String keyword,String productSn,Integer productCategoryId,
                  Integer brandId,Integer publishStatus,Integer verifyStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(keyword)){
            queryWrapper.like("name",keyword);
        }
        if (productSn!=null&&productSn!=""){
            queryWrapper.eq("product_sn",productSn);
        }
        if (productCategoryId!=null){
            queryWrapper.eq("product_category_id",productCategoryId);
        }
        if (brandId!=null){
            queryWrapper.eq("brand_id",brandId);
        }
        if (publishStatus!=null){
            queryWrapper.eq("publish_status",publishStatus);
        }
        if (verifyStatus!=null){
            queryWrapper.eq("verify_status",verifyStatus);
        }
        return R.ok(productService.page(page,queryWrapper));
    }
    @PostMapping("update/deleteStatus")
    public R updateDeleteStatus(@RequestParam String ids,@RequestParam Integer deleteStatus){
//        List<String> idList = Arrays.asList(ids.split(","));
//        for (String id : idList) {
//            Product product = new Product();
//            product.setDeleteStatus(deleteStatus);
//            product.setId(Long.valueOf(Integer.valueOf(id)));
//            productService.updateById(product,true);
//        }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("id",ids.split(","));
            queryWrapper.select("id","delete_status");
            List<Product> productList = productService.list(queryWrapper);
            for (Product product : productList) {
                product.setDeleteStatus(deleteStatus);
            }
            return R.ok(productService.updateBatch(productList,true));
    }

    /**
     * 退货的商品重新入库
     * @param pid 商品id
     * @param num 退货数量
     * @return
     */
    @PostMapping("restocking")
    public R restocking(@RequestParam Long pid,@RequestParam Integer num){
        return R.ok(productService.updateStock(pid,num));
    }
}
